package com.chengqj.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.mongo.MongoProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.mongodb.MongoDatabaseFactory;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.SimpleMongoClientDbFactory;
import org.springframework.data.mongodb.core.convert.MappingMongoConverter;
import org.springframework.data.mongodb.repository.config.EnableMongoRepositories;

@Configuration
@EnableMongoRepositories(
        basePackages = "com.chengqj.order",
        mongoTemplateRef = "orderMongoTemplate")
public class OrderMongoConfig {
    @Bean(name = "orderMongoProperties")
    @ConfigurationProperties(prefix = "spring.data.mongodb.order")
    public MongoProperties secondaryMongoProperties() {
        return new MongoProperties();
    }

    @Bean(name = "orderMongoTemplate")
    public MongoTemplate secondaryMongoTemplate(@Qualifier(value = "orderMongoProperties") MongoProperties mongoProperties, @Autowired MappingMongoConverter mappingMongoConverter) {
        return new MongoTemplate(secondaryFactory(mongoProperties), mappingMongoConverter);
    }
    @Bean(name = "orderMongoDbFactory")
    public MongoDatabaseFactory secondaryFactory(MongoProperties mongoProperties) {
        return new SimpleMongoClientDbFactory(mongoProperties.getUri());
    }
}
